

package com.soft.bams.client;
import com.soft.bams.entity.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.SocketException;

import javax.swing.*;
/**
 * @author 23838
 */
public class BusinessPanel extends JPanel {
    private ATMClient atm;

    public BusinessPanel(ATMClient atm) {
        this.atm = atm;
        initComponents();

    }


    //初始化业务界面
    public void initBusiness(){
        //使用 atm.acc 共有账户资源 进行初始化数据
        IDText.setText(atm.acc.getId()+"");
        nameText.setText(atm.acc.getName());
        balanceText.setText(atm.acc.getBalance()+"");
        moneyTextField.setText(0.0+"");

        //透支额度
        if(atm.acc instanceof CreditAccount){
            ceilingText.setText(((CreditAccount) atm.acc).getCeiling() +"");
        }else{
            ceilingText.setText("该账户不支持透支操作");
        }

        //贷款额度
        if(atm.acc instanceof LoanSavingAccount){
            loanText.setText(((LoanSavingAccount) atm.acc).getLoanAmount() +"");
        }else if(atm.acc instanceof LoanCreditAccount){
            loanText.setText(((LoanCreditAccount) atm.acc).getLoanAmount() +"");
        }else{
            loanText.setText("该账户不支持贷款操作");
        }

        //下拉菜单  不同账户类型，展示不同的可操作项
        if(atm.acc instanceof LoanSavingAccount){
            comboBox.setModel(new DefaultComboBoxModel<>(new String[] {"存款","取款","转账","贷款","还贷"}));
        }else if(atm.acc instanceof LoanCreditAccount){
            comboBox.setModel(new DefaultComboBoxModel<>(new String[] {"存款","取款","转账","修改透支额度","贷款","还贷"}));
        }else if(atm.acc instanceof CreditAccount){
            comboBox.setModel(new DefaultComboBoxModel<>(new String[] {"存款","取款","转账","修改透支额度"}));
        }else if(atm.acc instanceof SavingAccount){
            comboBox.setModel(new DefaultComboBoxModel<>(new String[] {"存款","取款","转账"}));
        }
    }
    //提交业务操作  执行Bank业务 -> 刷新页面
    private void submit(ActionEvent e) {
        Long id = Long.valueOf(IDText.getText());
        String moneyText = moneyTextField.getText();
        String select = comboBox.getSelectedItem().toString();
        if(moneyText==null || moneyText.equals("")){
            JOptionPane.showMessageDialog(null , "操作金额不能为空");
            return;
        }
        double money = Double.parseDouble(moneyText);
        if(money<=0.0){
            JOptionPane.showMessageDialog(null , "操作金额不能<=0.0");
            return;
        }
        Account acc = null;
        switch (select){
            /*case "存款":
                try {
                    acc = atm.bank.deposit(id , money);
                } catch (NotFoundException | MoneyException ex) {
                    JOptionPane.showMessageDialog(null , ex.getMessage());
                    moneyTextField.setText(0.0+"");
                }
                break;*/
            case "存款":
                // 构造指令：deposit#账户ID#金额
                String depositCmd = "deposit#" + id + "#" + money;
                atm.pw.println(depositCmd);
                atm.pw.flush();

                try {
                    Object response = atm.ois.readObject();
                    if (response instanceof Account) {
                        acc = (Account) response;
                        JOptionPane.showMessageDialog(null, "存款成功！");
                    } else if (response instanceof String) {
                        JOptionPane.showMessageDialog(null, (String) response);
                        moneyTextField.setText("0.0");
                    }
                } catch (SocketException ex) {
                    JOptionPane.showMessageDialog(this, "服务器已关闭");
                    System.exit(0);
                } catch (IOException | ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                }
                break;
            /*case "取款":
                String pwd=JOptionPane.showInputDialog(null,"请输入您的密码：");
                try {
                    acc=atm.bank.withdraw(id,pwd,money);
                } catch (NotFoundException |MoneyException|BalanceNotEnoughException ex) {
                    JOptionPane.showMessageDialog(null , ex.getMessage());
                    moneyTextField.setText(0.0+"");
                }
                break;*/
            case "取款":
                String pwd = JOptionPane.showInputDialog(null, "请输入您的密码：");
                // 构造指令：withdraw#账户ID#密码#金额
                String withdrawCmd = "withdraw#" + id + "#" + pwd + "#" + money;
                atm.pw.println(withdrawCmd);
                atm.pw.flush();

                try {
                    Object response = atm.ois.readObject();
                    if (response instanceof Account) {
                        acc = (Account) response;
                        JOptionPane.showMessageDialog(null, "取款成功！");
                    } else if (response instanceof String) {
                        JOptionPane.showMessageDialog(null, (String) response);
                        moneyTextField.setText("0.0");
                    }
                } catch (SocketException ex) {
                    JOptionPane.showMessageDialog(this, "服务器已关闭");
                    System.exit(0);
                } catch (IOException | ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                }
                break;
            case "转账":
                String toIdStr = JOptionPane.showInputDialog(null , "请输入转入账户ID信息：");
                String password = JOptionPane.showInputDialog(null , "请输入您的密码：");
                if(toIdStr.matches("\\d+")){

                    //1.写指令
                    atm.pw.println("transfer#"+id+"#"+password+"#"+toIdStr+"#"+money);
                    atm.pw.flush();

                    //2.读结果
                    try {
                        Object o = atm.ois.readObject();
                        if(o instanceof Account){
                            acc = (Account) o;
                            JOptionPane.showMessageDialog(null, "转账成功！");
                        }else if(o instanceof String){
                            JOptionPane.showMessageDialog(this , o);
                            moneyTextField.setText(0.0+"");
                        }
                    } catch (SocketException ex){
                        JOptionPane.showMessageDialog(this , "服务器已关闭");
                        System.exit(0);
                    } catch (IOException | ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }

                }
                break;
            /*case "修改透支额度":
                pwd=JOptionPane.showInputDialog(null,"请输入您的密码：");
                try {
                    acc=atm.bank.updateCeling(id, money, pwd);
                } catch (NotFoundException |MoneyException|TypeException ex) {
                    JOptionPane.showMessageDialog(null , ex.getMessage());
                    moneyTextField.setText(0.0+"");
                }
                break;*/
            case "修改透支额度":
                String updatePwd = JOptionPane.showInputDialog(null, "请输入您的密码：");
                // 构造指令：updateCeiling#账户ID#新额度#密码
                String updateCmd = "updateCeiling#" + id + "#" + money + "#" + updatePwd;
                atm.pw.println(updateCmd);
                atm.pw.flush();

                try {
                    Object response = atm.ois.readObject();
                    if (response instanceof Account) {
                        acc = (Account) response;
                        JOptionPane.showMessageDialog(null, "透支额度更新成功！");
                    } else if (response instanceof String) {
                        JOptionPane.showMessageDialog(null, (String) response);
                        moneyTextField.setText("0.0");
                    }
                } catch (SocketException ex) {
                    JOptionPane.showMessageDialog(this, "服务器已关闭");
                    System.exit(0);
                } catch (IOException | ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                }
                break;
           /* case "贷款":
                try {
                    acc=atm.bank.requestLoan(money,id);
                } catch (NotFoundException |MoneyException|TypeException ex) {
                    JOptionPane.showMessageDialog(null , ex.getMessage());
                    moneyTextField.setText(0.0+"");
                }
                break;*/
            case "贷款":
                // 构造指令：requestLoan#账户ID#贷款金额
                String loanCmd = "requestLoan#" + id + "#" + money;
                atm.pw.println(loanCmd);
                atm.pw.flush();

                try {
                    Object response = atm.ois.readObject();
                    if (response instanceof Account) {
                        acc = (Account) response;
                        JOptionPane.showMessageDialog(null, "贷款成功！");
                    } else if (response instanceof String) {
                        JOptionPane.showMessageDialog(null, (String) response);
                        moneyTextField.setText("0.0");
                    }
                } catch (SocketException ex) {
                    JOptionPane.showMessageDialog(this, "服务器已关闭");
                    System.exit(0);
                } catch (IOException | ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                }
                break;
            /*case "还贷":
                try {
                    acc=atm.bank.payLoan(money,id);
                } catch (NotFoundException |MoneyException|TypeException ex) {
                    JOptionPane.showMessageDialog(null , ex.getMessage());
                    moneyTextField.setText(0.0+"");
                }
                break;*/
            case "还贷":
                // 构造指令：payLoan#账户ID#还款金额
                String repayCmd = "payLoan#" + id + "#" + money;
                atm.pw.println(repayCmd);
                atm.pw.flush();

                try {
                    Object response = atm.ois.readObject();
                    if (response instanceof Account) {
                        acc = (Account) response;
                        JOptionPane.showMessageDialog(null, "还贷成功！");
                    } else if (response instanceof String) {
                        JOptionPane.showMessageDialog(null, (String) response);
                        moneyTextField.setText("0.0");
                    }
                } catch (SocketException ex) {
                    JOptionPane.showMessageDialog(this, "服务器已关闭");
                    System.exit(0);
                } catch (IOException | ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                }
                break;
        }

        //刷新界面
        if(acc!=null){
            atm.acc = acc;
            //初始化业务界面
            initBusiness();
        }
    }

    //跳转至主界面，并清空当前业务界面数据信息
    private void back(ActionEvent e) {
        CardLayout cardLayout = (CardLayout)this.getParent().getLayout();
        cardLayout.show(this.getParent() , "mainPanel");

        //清空当前业务界面数据信息
        clear();
    }

    //清空业务界面
    private void clear() {
        IDText.setText("");
        nameText.setText("");
        balanceText.setText("");
        ceilingText.setText("");
        loanText.setText("");
        comboBox.setSelectedIndex(0);
        moneyTextField.setText(0.0+"");
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        titlelabel = new JLabel();
        IDlabel = new JLabel();
        namelabel = new JLabel();
        balancelabel = new JLabel();
        ceilinglabel = new JLabel();
        loanlabel = new JLabel();
        comboBox = new JComboBox<>();
        moneyTextField = new JTextField();
        IDText = new JLabel();
        nameText = new JLabel();
        balanceText = new JLabel();
        ceilingText = new JLabel();
        loanText = new JLabel();
        submitbutton = new JButton();
        backbutton = new JButton();

        //======== this ========
        setLayout(null);

        //---- titlelabel ----
        titlelabel.setText("\u4e1a \u52a1 \u754c \u9762");
        titlelabel.setHorizontalAlignment(SwingConstants.CENTER);
        titlelabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 20));
        add(titlelabel);
        titlelabel.setBounds(180, 40, 135, 50);

        //---- IDlabel ----
        IDlabel.setText("\u8d26 \u6237 ID\uff1a");
        IDlabel.setHorizontalAlignment(SwingConstants.CENTER);
        IDlabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(IDlabel);
        IDlabel.setBounds(55, 100, 80, 30);

        //---- namelabel ----
        namelabel.setText("\u7528 \u6237 \u540d\uff1a");
        namelabel.setHorizontalAlignment(SwingConstants.CENTER);
        namelabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(namelabel);
        namelabel.setBounds(55, 150, 80, 30);

        //---- balancelabel ----
        balancelabel.setText("\u4f59   \u989d\uff1a");
        balancelabel.setHorizontalAlignment(SwingConstants.CENTER);
        balancelabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(balancelabel);
        balancelabel.setBounds(55, 195, 80, 30);

        //---- ceilinglabel ----
        ceilinglabel.setText("\u4fe1 \u7528 \u989d \u5ea6\uff1a");
        ceilinglabel.setHorizontalAlignment(SwingConstants.CENTER);
        ceilinglabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(ceilinglabel);
        ceilinglabel.setBounds(15, 245, 130, 30);

        //---- loanlabel ----
        loanlabel.setText("\u8d37 \u6b3e \u989d \u5ea6\uff1a");
        loanlabel.setHorizontalAlignment(SwingConstants.CENTER);
        loanlabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(loanlabel);
        loanlabel.setBounds(30, 295, 105, 30);

        //---- comboBox ----
        comboBox.setModel(new DefaultComboBoxModel<>(new String[] {
                "\u5b58\u6b3e",
                "\u53d6\u6b3e",
                "\u8f6c\u8d26",
                "\u4fee\u6539\u900f\u652f\u989d\u5ea6",
                "\u8d37\u6b3e",
                "\u8fd8\u8d37"
        }));
        comboBox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(comboBox);
        comboBox.setBounds(60, 355, 156, 35);

        //---- moneyTextField ----
        moneyTextField.setText("0.0");
        moneyTextField.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(moneyTextField);
        moneyTextField.setBounds(250, 355, 145, 35);

        //---- IDText ----
        IDText.setHorizontalAlignment(SwingConstants.CENTER);
        IDText.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(IDText);
        IDText.setBounds(180, 100, 170, 30);

        //---- nameText ----
        nameText.setHorizontalAlignment(SwingConstants.CENTER);
        nameText.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(nameText);
        nameText.setBounds(180, 150, 170, 30);

        //---- balanceText ----
        balanceText.setHorizontalAlignment(SwingConstants.CENTER);
        balanceText.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(balanceText);
        balanceText.setBounds(180, 200, 170, 30);

        //---- ceilingText ----
        ceilingText.setHorizontalAlignment(SwingConstants.CENTER);
        ceilingText.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(ceilingText);
        ceilingText.setBounds(180, 245, 170, 30);

        //---- loanText ----
        loanText.setHorizontalAlignment(SwingConstants.CENTER);
        loanText.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        add(loanText);
        loanText.setBounds(180, 295, 170, 30);

        //---- submitbutton ----
        submitbutton.setText("\u63d0\u4ea4");
        submitbutton.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        submitbutton.addActionListener(e -> submit(e));
        add(submitbutton);
        submitbutton.setBounds(120, 460, 78, 34);

        //---- backbutton ----
        backbutton.setText("\u8fd4\u56de");
        backbutton.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        backbutton.addActionListener(e -> back(e));
        add(backbutton);
        backbutton.setBounds(280, 460, 78, 34);

        setPreferredSize(new Dimension(500, 600));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel titlelabel;
    private JLabel IDlabel;
    private JLabel namelabel;
    private JLabel balancelabel;
    private JLabel ceilinglabel;
    private JLabel loanlabel;
    private JComboBox<String> comboBox;
    private JTextField moneyTextField;
    private JLabel IDText;
    private JLabel nameText;
    private JLabel balanceText;
    private JLabel ceilingText;
    private JLabel loanText;
    private JButton submitbutton;
    private JButton backbutton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
